package makovo.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Map;

import makovo.utils.DataAccess;
import makovo.data.User_Register;
import makovo.data.Resources;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class UserRegister extends ActionSupport implements SessionAware {

	private String mTextPattern = "^[a-zA-Z0-9áéíóúÁÉÍÓÚàèìòùÀÈÌÒÙñÑ \\-.]{1,}$";
	private String mEmailPattern = "^[a-zA-Z0-9.]{1,}[@]{1}[a-zA-Z0-9.]{1,}$";
	private String mNumberPatter = "^[0-9.,]{1,}$";
	private String mOnlyNumberPatter = "^[0-9]{1,}$";
	private String mDatePattern = "^[0-9]{4}/[0-9]{2}/[0-9]{2}$";

	private String mEmail;
	private String mName;
	private String mLastName;
	private String mSecondLastName;
	private int mTypeUsr;
	private String mPassword;
	private String mState;

	private int mStateId;
	private int mCityId;
	private int mlocId;

	public String getName() {
		return mName;
	}

	public void setName(String name) {
		try {
			mName = new String(name.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getLastName() {
		return this.mLastName;
	}

	public void setLastName(String lastName) {
		try {
			this.mLastName = new String(lastName.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getSecondLastName() {
		return this.mSecondLastName;
	}

	public void setSecondLastName(String secondLastName) {
		try {
			this.mSecondLastName = new String(secondLastName.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getEmail() {
		return mEmail;
	}

	public void setEmail(String email) {
		mEmail = email;
	}

	public int getTypeUsr() {
		return mTypeUsr;
	}

	public void setTypeUsr(int typeUsr) {
		mTypeUsr = typeUsr;
	}

	public int getStateId() {
		return mStateId;
	}

	public void setStateId(int stateId) {
		mStateId = stateId;
	}

	public int getCityId() {
		return mCityId;
	}

	public void setCityId(int cityId) {
		mCityId = cityId;
	}

	public int getLocId() {
		return mlocId;
	}

	public void setLocId(int locId) {
		mlocId = locId;
	}

	public String getState() {
		return mState;
	}

	public void setState(String state) {
		try {
			this.mState = new String(state.getBytes("ISO-8859-1"), "UTF-8").toUpperCase().substring(0, 2);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getPassword() {
		return mPassword;
	}

	public void setPassword(String password) {
		mPassword = password;
	}

	public String execute() {
		String accion = "SUCCESS";

		if (User_Register.registerUser(mEmail, mName, mLastName, mSecondLastName, mPassword, mState, mTypeUsr, mStateId, mCityId,
				mlocId)) {
			accion = "SUCCESS";
		}
		return accion;
	}

	public void validate() {
		if (!mEmail.matches(mEmailPattern))
			addActionError("Email del usuario");

		if (!mName.matches(mTextPattern))
			addActionError("Nombre del usuario");

		if (!mLastName.matches(mTextPattern))
			addActionError("Apellido paterno del usuario");

		if (!mPassword.matches(mTextPattern))
			addActionError("Password");

		if (!mSecondLastName.matches(mTextPattern))
			addActionError("Apellido materno del aplicante");

		if (mStateId == -1)
			addActionError("Estado");

		if (mCityId == -1)
			addActionError("Ciudad");

		if (mTypeUsr == -1)
			addActionError("Tipo de Usuario");
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub

	}
}
